package com.ajmd.onlineexam.service;

import com.ajmd.onlineexam.entity.*;
import com.ajmd.onlineexam.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ajmd
 * @since 2023/5/6 1:17
 */
@Service
public class ExamQuestionService {
    @Resource
    private ExamQuestionMapper examQuestionMapper;
    @Resource
    private TestPaperQuestionMapper tPQuestionMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private OptionService optionService;
    @Resource
    private OptionMapper optionMapper;
    @Resource
    private ImageMapper imageMapper;


    /**
     * 实例化试卷
     * @param examId
     * @param testPaperId
     * @return
     */
    public int copyTestPaper(String examId,String testPaperId){

        //根据 testPaperId 从试卷-试题表中查出所有记录
        LambdaQueryWrapper<TestPaperQuestionPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TestPaperQuestionPO::getTestPaperId,testPaperId);
        List<TestPaperQuestionPO> tPQuestionPOList = tPQuestionMapper.selectList(queryWrapper);
//        if(ObjectUtils.isEmpty(tPQuestionPOS)){//如果试卷内容为空，正常情况下前端是无法选择的
//            return 0;
//        }
        //抽取出试卷-试题id
        List<String> tPQuestionIds = tPQuestionPOList.stream().map(TestPaperQuestionPO::getQuestionId).collect(Collectors.toList());
        //tPQuestionPOList 转map
        Map<String, TestPaperQuestionPO> tPQuestionPOMap = tPQuestionPOList.stream().collect(Collectors.toMap(TestPaperQuestionPO::getId, Function.identity()));
        //根据 tPQuestionIds 查询所有选项
        LambdaQueryWrapper<OptionPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(OptionPO::getBelongQuestionId,tPQuestionIds);
        List<OptionPO> allOptionPOList = optionMapper.selectList(lambdaQueryWrapper);
        //allOptionPOList 根据questionId分组
        Map<String, List<OptionPO>> optionListMap = new HashMap<>();
        if(ObjectUtils.isNotEmpty(allOptionPOList)){
            optionListMap = allOptionPOList.stream().collect(Collectors.groupingBy(OptionPO::getBelongQuestionId));
        }
        //根据 questionIds 查询所有图片
        LambdaQueryWrapper<ImagePO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ImagePO::getBelongId,tPQuestionIds);
        List<ImagePO> allImagePOList = imageMapper.selectList(wrapper);
        //根据所属二级对象分组
        Map<String, List<ImagePO>> imagePOListMap = new HashMap<>();
        if(ObjectUtils.isNotEmpty(allImagePOList)){
            imagePOListMap = allImagePOList.stream().collect(Collectors.groupingBy(ImagePO::getBelongSecondlyId));
        }

        List<OptionPO> optionPOListAdd = new ArrayList<>();
        List<ImagePO> imagePOListAdd = new ArrayList<>();

        //实例化试题到考试试题表
        List<ExamQuestionPO> examQuestionPOList = new ArrayList<>();
        for(TestPaperQuestionPO tPQuestionPO:tPQuestionPOList){
            ExamQuestionPO examQuestionPO = new ExamQuestionPO();
            //.....其他信息分组，排序，试题信息。。。
            BeanUtils.copyProperties(tPQuestionPO,examQuestionPO);

            examQuestionPO.setRightRate(BigDecimal.valueOf(0));//初始化正确率
            examQuestionPO.setExamId(examId);
            String examQuestionId = UUID.randomUUID().toString();//考试试题id
            examQuestionPO.setId(examQuestionId);
            examQuestionPOList.add(examQuestionPO);

            //实例化试题对应的选项
            List<OptionPO> optionPOList = optionListMap.get(tPQuestionPO.getQuestionId());//试卷试题id
            if(ObjectUtils.isNotEmpty(optionPOList)){
                for(OptionPO optionPO:optionPOList){
                    OptionPO optionPOAdd = new OptionPO();
                    BeanUtils.copyProperties(optionPO,optionPOAdd);
                    String examOptionId = UUID.randomUUID().toString();
                    optionPOAdd.setId(examOptionId);
                    optionPOAdd.setBelongQuestionId(examQuestionId);
                    optionPOListAdd.add(optionPOAdd);
                    //实例化选项图片
                    List<ImagePO> optionImagePOList = imagePOListMap.get(optionPO.getId());
                    if(ObjectUtils.isNotEmpty(optionImagePOList)){
                        for(ImagePO imagePO:optionImagePOList){
                            ImagePO imagePOAdd = new ImagePO();
                            imagePOAdd.setId(UUID.randomUUID().toString());
                            imagePOAdd.setBelongId(examQuestionId);
                            imagePOAdd.setBelongSecondlyId(examOptionId);
                            imagePOAdd.setBelongType(imagePO.getBelongType());
                            imagePOAdd.setPath(imagePO.getPath());
                            imagePOListAdd.add(imagePOAdd);
                        }
                    }
                }
            }
            //实例化试题图片
            List<ImagePO> questionImagePOList = imagePOListMap.get(tPQuestionPO.getQuestionId());
            if(ObjectUtils.isNotEmpty(questionImagePOList)){
                for(ImagePO imagePO:questionImagePOList){
                    ImagePO imagePOAdd = new ImagePO();
                    imagePOAdd.setId(UUID.randomUUID().toString());
                    imagePOAdd.setBelongId(examQuestionId);
                    imagePOAdd.setBelongSecondlyId(examQuestionId);
                    imagePOAdd.setBelongType(imagePO.getBelongType());
                    imagePOAdd.setPath(imagePO.getPath());
                    imagePOListAdd.add(imagePOAdd);
                }
            }
        }

        //保存选项
        if(ObjectUtils.isNotEmpty(optionPOListAdd)){
            optionMapper.insertBatchSomeColumn(optionPOListAdd);
        }
        //保存图片
        if(ObjectUtils.isNotEmpty(imagePOListAdd)){
            imageMapper.insertBatchSomeColumn(imagePOListAdd);
        }
        //保存考试试题
        return examQuestionMapper.insertBatchSomeColumn(examQuestionPOList);
    }




    /**
     * 根据考试id,删除试卷试题及关联的选项、图片
     * @param examId
     * @return
     */
    public int deleteExamQuestions(String examId){
        LambdaQueryWrapper<ExamQuestionPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamQuestionPO::getExamId,examId);
        List<ExamQuestionPO> examQuestionPOList = examQuestionMapper.selectList(queryWrapper);
        List<String> examQuestionIds = examQuestionPOList.stream().map(ExamQuestionPO::getId).collect(Collectors.toList());
        //删除试题下的选项和图片
        optionService.deleteOptionAndImg(examQuestionIds);
        //删除试题
        return examQuestionMapper.deleteBatchIds(examQuestionIds);

    }
}
